﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ttlang.semantic
{
    //一个模块代表一个DLL
    public class Module
    {
        public string name;
        public List<Module> dependModules;
        //public Dictionary<string, TTType> types;
        //public Dictionary<string, Member_Method> methods;
        public TTNameSpace _defNameSpace;
        public Module(string name)
        {
            this.name = name;
            dependModules = new List<Module>();
            dependModules.Add(SystemModule.ttlangCore);
            //types = new Dictionary<string, TTType>();
            //methods = new Dictionary<string, Member_Method>();
            _defNameSpace = new TTNameSpace(this, null, "");
        }
        public static List<Module> GetAllModules(Module from)
        {
            List<Module> module = new List<Module>();
            module.Add(from);

            int mcountlast = module.Count;

            do
            {
                mcountlast = module.Count;
                List<Module> clone = new List<Module>(module);
                foreach (var m in clone)
                {
                    if (m.dependModules != null)
                    {
                        foreach (var mc in m.dependModules)
                        {
                            if (module.Contains(mc) == false)
                            {
                                module.Add(mc);
                            }
                        }
                    }
                }
            }
            while (module.Count == mcountlast);
            return module;
        }
        public bool FindMethod(string id, out semantic.TTMethod method)
        {
            //应该遍历，还没做完
            method = null;
            if (_defNameSpace.methods == null)
                return false;
            return _defNameSpace.methods.TryGetValue(id, out method);
        }
        public bool FindVar(string id, out semantic.TTVar var)
        {
            //应该遍历，还没做完
            var = null;
            if (_defNameSpace.vars == null)
                return false;
            return _defNameSpace.vars.TryGetValue(id, out var);

        }
        public bool FindType(string id, out semantic.TTType type)
        {
            //应该遍历，还没做完
            type = null;
            if (_defNameSpace.types == null)
                return false;

            return _defNameSpace.types.TryGetValue(id, out type);
        }
        public bool FindNameSpace(string id,out semantic.TTNameSpace type)
        {
            type = null;
            if (_defNameSpace.namespaces == null)
                return false;

            return _defNameSpace.namespaces.TryGetValue(id, out type);
        }

    }

}
